package com.shop.cereshop.app.service.buyer.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.cereshop.app.dao.buyer.UserStopSpeeckDAO;
import com.shop.cereshop.app.param.buyer.UserManageParam;
import com.shop.cereshop.app.service.buyer.UserStopSpeeckService;
import com.shop.cereshop.commons.domain.buyer.UserStopSpeeck;
import com.shop.cereshop.commons.domain.im.tx.req.user.SetGlobalBanReq;
import com.shop.cereshop.commons.domain.im.tx.resp.ImBaseResp;
import com.shop.cereshop.commons.service.im.ImUserService;
import com.shop.cereshop.commons.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service
public class UserStopSpeeckServiceImpl implements UserStopSpeeckService {
    @Autowired
    private UserStopSpeeckDAO userStopSpeeckDAO;

    @Autowired
    UserStopSpeeckService userStopSpeeckService;


    @Autowired
    ImUserService imUserService;

    @Override
    public UserStopSpeeck selectById(Long buyerId) {
        UserStopSpeeck userStopSpeeck = userStopSpeeckDAO.selectById(buyerId);
        return userStopSpeeck;
    }

    @Override
    public Integer userStopSpeeck(UserManageParam userManageParam) {
//        获取当前时间
        Long nowTime = System.currentTimeMillis();
        if (userManageParam.getAccount() != null) {
            UserStopSpeeck userSpeeck = userStopSpeeckDAO.selectByUserId(Long.valueOf(userManageParam.getAccount()));
            if (userSpeeck != null) {
                if (userManageParam.getC2CmsgNospeakingTime() != null && userManageParam.getC2CmsgNospeakingTime() > 0) {
                    // 将秒转换为毫秒后相加,得到用户禁言时长
                    Long c2CmsgNospeakingTime = userManageParam.getC2CmsgNospeakingTime() * 1000 + nowTime;
                    userSpeeck.setPrivateChatTime(timestampToLocalDateTime(c2CmsgNospeakingTime));
//                        设置私聊禁言状态为1，即已禁言
                    userSpeeck.setPrivateChat(1);
                }
                if (userManageParam.getGroupmsgNospeakingTime() != null && userManageParam.getGroupmsgNospeakingTime() > 0) {
//
                    // 将秒转换为毫秒后相加,得到用户禁言时
                    Long groupmsgNospeakingTime = userManageParam.getGroupmsgNospeakingTime() * 1000 + nowTime;
                    userSpeeck.setGroupChatTime(timestampToLocalDateTime(groupmsgNospeakingTime));
//                        设置群聊禁言状态为1，即已禁言
                    userSpeeck.setGroupChat(1);
                }
                userSpeeck.setUpdateTime(LocalDateTime.now());
                QueryWrapper<UserStopSpeeck> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("user_id", userManageParam.getAccount());

                return userStopSpeeckDAO.update(userSpeeck, queryWrapper);
            }
//                向数据库中插入禁言信息
            else {
                userSpeeck = new UserStopSpeeck();
                if (userManageParam.getC2CmsgNospeakingTime() != null) {
                    Long c2CmsgNospeakingTime = userManageParam.getC2CmsgNospeakingTime() * 1000 + nowTime;
                    userSpeeck.setPrivateChatTime(timestampToLocalDateTime(c2CmsgNospeakingTime));
                    userSpeeck.setPrivateChat(1);
                }
                if (userManageParam.getGroupmsgNospeakingTime() != null) {
                    // 将秒转换为毫秒后相加,得到用户禁言时
                    Long groupmsgNospeakingTime = userManageParam.getGroupmsgNospeakingTime() * 1000 + nowTime;
                    userSpeeck.setGroupChatTime(timestampToLocalDateTime(groupmsgNospeakingTime));
                    userSpeeck.setGroupChat(1);

                }
//                    获取当前时间
                userSpeeck.setCreatTime(LocalDateTime.now());
                userSpeeck.setUpdateTime(LocalDateTime.now());


                userSpeeck.setUserId(Long.valueOf(userManageParam.getAccount()));

                return userStopSpeeckDAO.insert(userSpeeck);
            }

        }

        return 0;
    }

    @Override
    public Integer deleteByUsers(List<Long> buyerId) {
//        批量删除用户禁言结束的用户数据
        QueryWrapper<UserStopSpeeck> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", buyerId);
        return userStopSpeeckDAO.delete(queryWrapper);
    }

    @Override
    public Integer updateUserStopSpeeckStatusPrivate(List<Long> buyerId) {
//        批量修改用户单聊禁言结束的数据
        return userStopSpeeckDAO.updateStatusPrivate(buyerId);

    }

    @Override
    public Integer updateUserStopSpeeckStatusGroup(List<Long> buyerId) {
        return userStopSpeeckDAO.updateStatusGroup(buyerId);
    }

    @Override
    public Integer updateState(UserStopSpeeck userStopSpeeck) {
        return userStopSpeeckDAO.updateState(userStopSpeeck);

    }

    @Override
    public Integer spanSpeech(UserManageParam userManageParam) {
        //    解除用户私聊禁言
        if (userManageParam.getC2CmsgNospeakingTime() != null && userManageParam.getStopSpeeckType() == 1 && userManageParam.getAccount() != null && !(userManageParam.getAccount().equals(""))  && userManageParam.getC2CmsgNospeakingTime() == 0) {
            SetGlobalBanReq setGlobalBanReq = new SetGlobalBanReq();
//            获取当前时间的毫秒级的时间戳
            Long nowTime = System.currentTimeMillis();
            setGlobalBanReq.setSetAccount(userManageParam.getAccount());
            setGlobalBanReq.setC2CmsgNospeakingTime(0L);
            ImBaseResp imBaseResp = imUserService.setGlobalBan(setGlobalBanReq);
            if (imBaseResp.getErrorCode() == 0) {
                UserStopSpeeck userStopSpeeck = new UserStopSpeeck();
//                    更新数据库的状态
                userStopSpeeck.setUserId(Long.valueOf(userManageParam.getAccount()));
                userStopSpeeck.setPrivateChat(0);
                userStopSpeeck.setUpdateTime(TimeUtils.timestampToLocalDateTime(nowTime));
                userStopSpeeckService.updateState(userStopSpeeck);
//                把私聊禁言时间置为null
                return userStopSpeeckDAO.updatePrivateChatTime(Long.valueOf(userManageParam.getAccount()));
            }

        }
//        解除用户群聊禁言
        else {
            SetGlobalBanReq setGlobalBanReq = new SetGlobalBanReq();
            if (userManageParam.getStopSpeeckType() == 1 && userManageParam.getAccount() != null && !(userManageParam.getAccount().equals("")) && userManageParam.getGroupmsgNospeakingTime()!= null&& userManageParam.getGroupmsgNospeakingTime() == 0) {
//            获取当前时间的毫秒级时间戳
                Long nowTime = System.currentTimeMillis();
                setGlobalBanReq.setSetAccount(userManageParam.getAccount());
                setGlobalBanReq.setGroupmsgNospeakingTime(0L);
                ImBaseResp imBaseResp = imUserService.setGlobalBan(setGlobalBanReq);
                if (imBaseResp.getErrorCode() == 0) {
                    UserStopSpeeck userStopSpeeck = new UserStopSpeeck();
                    userStopSpeeck.setUserId(Long.valueOf(userManageParam.getAccount()));
                    userStopSpeeck.setGroupChat(0);
                    userStopSpeeck.setUpdateTime(TimeUtils.timestampToLocalDateTime(nowTime));
                    userStopSpeeckService.updateState(userStopSpeeck);

                    return userStopSpeeckDAO.updateGroupChatTime(Long.valueOf(userManageParam.getAccount()));
                }

            }

        }
        return 0;
    }


    public static LocalDateTime timestampToLocalDateTime(Long timestamp) {
        return Instant.ofEpochMilli(timestamp)
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
    }

}
